<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Content.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Content extends Engine_Db_Table {
	
	protected $_serializedColumns = array ('params' );
	
	/**
	 * Set profile page default widget in user content table without tab
	 *
	 * @param string $name
	 * @param int $page_id
	 * @param string $title
	 * @param int $titleCount
	 * @param int $order
	 */
	public function setDefaultInfo($name = null, $contentpage_id, $title = null, $titleCount = null, $order = null) {
		if (! empty ( $name )) {
			$select = $this->select ();
			$select_content = $select->from ( $this->info ( 'name' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', $name )->limit ( 1 );
			$content = $select_content->query ()->fetchAll ();
			if (empty ( $content )) {
				$select = $this->select ();
				$select_container = $select->from ( $this->info ( 'name' ), array ('content_id' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'container' )->limit ( 1 );
				$container = $select_container->query ()->fetchAll ();
				if (! empty ( $container )) {
					$select = $this->select ();
					$container_id = $container [0] ['content_id'];
					$select_middle = $select->from ( $this->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
					$middle = $select_middle->query ()->fetchAll ();
					if (! empty ( $middle )) {
						$select = $this->select ();
						$middle_id = $middle [0] ['content_id'];
						$select_tab = $select->from ( $this->info ( 'name' ) )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'contentpage_id = ?', $contentpage_id )->limit ( 1 );
						$tab = $select_tab->query ()->fetchAll ();
						if (! empty ( $tab )) {
							$tab_id = $tab [0] ['content_id'];
						}
						$contentWidget = $this->createRow ();
						$contentWidget->contentpage_id = $contentpage_id;
						$contentWidget->type = 'widget';
						$contentWidget->name = $name;
						$contentWidget->parent_content_id = ($tab_id ? $tab_id : $middle_id);
						$contentWidget->order = $order;
						$contentWidget->params = '{"title":"' . $title . '" , "titleCount":"' . $titleCount . '"}';
						$contentWidget->save ();
					}
				}
			}
		}
	}
	
	/**
	 * Set profile page default widget in user content table with tab
	 *
	 * @param string $name
	 * @param int $page_id
	 * @param string $title
	 * @param int $titleCount
	 * @param int $order
	 */
	public function setContentDefaultInfoWithoutTab($name = null, $contentpage_id, $title = null, $titleCount = null, $order = null) {
		if (! empty ( $name )) {
			$select = $this->select ();
			$select_content = $select->from ( $this->info ( 'name' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', $name )->limit ( 1 );
			$content = $select_content->query ()->fetchAll ();
			if (empty ( $content )) {
				$select = $this->select ();
				$select_container = $select->from ( $this->info ( 'name' ), array ('content_id' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'container' )->limit ( 1 );
				$container = $select_container->query ()->fetchAll ();
				if (! empty ( $container )) {
					$select = $this->select ();
					$container_id = $container [0] ['content_id'];
					$select_middle = $select->from ( $this->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
					$middle = $select_middle->query ()->fetchAll ();
					if (! empty ( $middle )) {
						$middle_id = $middle [0] ['content_id'];
						$contentWidget = $this->createRow ();
						$contentWidget->contentpage_id = $contentpage_id;
						$contentWidget->type = 'widget';
						$contentWidget->name = $name;
						$contentWidget->parent_content_id = ($middle_id);
						$contentWidget->order = $order;
						$contentWidget->params = '{"title":"' . $title . '" , "titleCount":"' . $titleCount . '"}';
						$contentWidget->save ();
					}
				}
			}
		}
	}
	
	/**
	 * Set default widget in content pages table
	 *
	 * @param object $table
	 * @param string $tablename
	 * @param int $page_id
	 * @param string $type
	 * @param string $widgetname
	 * @param int $middle_id 
	 * @param int $order 
	 * @param string $title 
	 * @param int $titlecount    
	 */
	public function setDefaultDataUserWidget($table, $tablename, $contentpage_id, $type, $widgetname, $middle_id, $order, $title = null, $titlecount = null) {
		$selectWidgetId = $this->select ()->where ( 'contentpage_id =?', $contentpage_id )->where ( 'type = ?', $type )->where ( 'name = ?', $widgetname )->where ( 'parent_content_id = ?', $middle_id )->limit ( 1 );
		$fetchWidgetContentId = $selectWidgetId->query ()->fetchAll ();
		if (empty ( $fetchWidgetContentId )) {
			$contentWidget = $this->createRow ();
			$contentWidget->contentpage_id = $contentpage_id;
			$contentWidget->type = $type;
			$contentWidget->name = $widgetname;
			$contentWidget->parent_content_id = $middle_id;
			$contentWidget->order = $order;
			$contentWidget->params = "{\"title\":\"$title\", \"titleCount\":\"$titlecount\"}";
			$contentWidget->save ();
		}
	}
	
	/**
	 * Set default widget in content pages table without tab
	 *
	 * @param int $page_id    
	 */
	public function setWithoutTabLayout($page_id) {
		
		// GET CONTENT TABLE
		$contentTable = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' );
		
		// GET CONTENT TABLE NAME
		$contentTableName = $this->info ( 'name' );
		
		//INSERTING MAIN CONTAINER
		$mainContainer = $this->createRow ();
		$mainContainer->contentpage_id = $page_id;
		$mainContainer->type = 'container';
		$mainContainer->name = 'main';
		$mainContainer->order = 2;
		$mainContainer->save ();
		$container_id = $mainContainer->content_id;
		
		//INSERTING MAIN-MIDDLE CONTAINER
		$mainMiddleContainer = $this->createRow ();
		$mainMiddleContainer->contentpage_id = $page_id;
		$mainMiddleContainer->type = 'container';
		$mainMiddleContainer->name = 'middle';
		$mainMiddleContainer->parent_content_id = $container_id;
		$mainMiddleContainer->order = 6;
		$mainMiddleContainer->save ();
		$middle_id = $mainMiddleContainer->content_id;
		
		//INSERTING MAIN-LEFT CONTAINER
		$mainLeftContainer = $this->createRow ();
		$mainLeftContainer->contentpage_id = $page_id;
		$mainLeftContainer->type = 'container';
		$mainLeftContainer->name = 'left';
		$mainLeftContainer->parent_content_id = $container_id;
		$mainLeftContainer->order = 4;
		$mainLeftContainer->save ();
		$left_id = $mainLeftContainer->content_id;
		
		//INSERTING TITLE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.title-sitepage', $middle_id, 2 );
		
		//INSERTING LIKE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like-button', $middle_id, 3 );
		
		//INSERTING FACEBOOK LIKE WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'facebookse' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', $middle_id, 4 );
		}
		
		//INSERTING PHOTO STRIP WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.photorecent-sitepage', $middle_id, 5 );
		}
		
		//INSERTING ACTIVITY FEED WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'socialengineaddon.feed', $middle_id, 6, 'Updates', 'true' );
		
		//INSERTING INFORAMTION WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.info-sitepage', $middle_id, 7, 'Info', 'true' );
		
		//INSERTING OVERVIEW WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.overview-sitepage', $middle_id, 8, 'Overview', 'true' );
		
		//INSERTING LOCATION WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.location-sitepage', $middle_id, 9, 'Map', 'true' );
		
		//INSERTING LINKS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'core.profile-links', $middle_id, 125, 'Links', 'true' );
		
		//INSERTING MAIN PHOTO WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.mainphoto-sitepage', $left_id, 10 );
		
		//INSERTING WIDGET LINK WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.widgetlinks-sitepage', $left_id, 11 );
		
		//INSERTING OPTIONS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.options-sitepage', $left_id, 12 );
		
		//INSERTING WRITE SOMETHING ABOUT WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.write-page', $left_id, 13 );
		
		//INSERTING WRITE SOMETHING ABOUT WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.information-sitepage', $left_id, 14, 'Information', 'true' );
		
		//INSERTING LIKE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like', $left_id, 15 );
		
		//INSERTING RATING WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', $left_id, 16, 'Ratings' );
		}
		
		//INSERTING BADGE WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', $left_id, 17, 'Badge' );
		}
		
		//INSERTING SOCIAL SHARE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.socialshare-sitepage', $left_id, 19, 'Social Share' );
		
		//INSERTING FOUR SQUARE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.foursquare-sitepage', $left_id, 20 );
		
		//INSERTING INSIGHTS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.insights-sitepage', $left_id, 22, 'Insights' );
		
		//INSERTING FEATURED OWNER WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.featuredowner-sitepage', $left_id, 23, 'Owners' );
		
		//INSERTING ALBUM WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.albums-sitepage', $left_id, 24, 'Albums' );
			$this->setContentDefaultInfoWithoutTab ( 'sitepage.photos-sitepage', $page_id, 'Photos', 'true', '110' );
		}
		
		//INSERTING PAGE PROFILE PLAYER WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagemusic.profile-player', $left_id, 25 );
		}
		
		//INSERTING LINKED PAGES WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.favourite-page', $left_id, 26, 'Linked Pages' );
		
		//INSERTING VIDEO WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagevideo' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagevideo.profile-sitepagevideos', $page_id, 'Videos', 'true', '111' );
		}
		
		//INSERTING NOTE WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagenote' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagenote.profile-sitepagenotes', $page_id, 'Notes', 'true', '112' );
		}
		
		//INSERTING REVIEW WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagereview.profile-sitepagereviews', $page_id, 'Reviews', 'true', '113' );
		}
		
		//INSERTING FORM WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageform' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepageform.sitepage-viewform', $page_id, 'Form', 'false', '114' );
		}
		
		//INSERTING DOCUMENT WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagedocument' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagedocument.profile-sitepagedocuments', $page_id, 'Documents', 'true', '115' );
		}
		
		//INSERTING OFFER WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageoffer' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepageoffer.profile-sitepageoffers', $page_id, 'Offers', 'true', '116' );
		}
		
		//INSERTING EVENT WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageevent' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepageevent.profile-sitepageevents', $page_id, 'Events', 'true', '117' );
		}
		
		//INSERTING POLL WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagepoll' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagepoll.profile-sitepagepolls', $page_id, 'Polls', 'true', '118' );
		}
		
		//INSERTING DISCUSSION WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepage.discussion-sitepage', $page_id, 'Discussions', 'true', '119' );
		}
		
		//INSERTING NOTE WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
			$this->setContentDefaultInfoWithoutTab ( 'sitepagemusic.profile-sitepagemusic', $page_id, 'Music', 'true', '120' );
		}
	}
	
	public function setTabbedLayout($page_id) {
		
		//SHOW HOW MANY TAB SHOULD BE SHOW IN THE PAGE PROFILE PAGE BEFORE MORE LINK
		$showmaxtab = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.showmore', 8 );
		
		// GET CONTENT TABLE
		$contentTable = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' );
		
		// GET CONTENT TABLE NAME
		$contentTableName = $this->info ( 'name' );
		
		//INSERTING MAIN CONTAINER
		$mainContainer = $this->createRow ();
		$mainContainer->contentpage_id = $page_id;
		$mainContainer->type = 'container';
		$mainContainer->name = 'main';
		$mainContainer->order = 2;
		$mainContainer->save ();
		$container_id = $mainContainer->content_id;
		
		//INSERTING MAIN-MIDDLE CONTAINER
		$mainMiddleContainer = $this->createRow ();
		$mainMiddleContainer->contentpage_id = $page_id;
		$mainMiddleContainer->type = 'container';
		$mainMiddleContainer->name = 'middle';
		$mainMiddleContainer->parent_content_id = $container_id;
		$mainMiddleContainer->order = 6;
		$mainMiddleContainer->save ();
		$middle_id = $mainMiddleContainer->content_id;
		
		//INSERTING MAIN-LEFT CONTAINER
		$mainLeftContainer = $this->createRow ();
		$mainLeftContainer->contentpage_id = $page_id;
		$mainLeftContainer->type = 'container';
		$mainLeftContainer->name = 'left';
		$mainLeftContainer->parent_content_id = $container_id;
		$mainLeftContainer->order = 4;
		$mainLeftContainer->save ();
		$left_id = $mainLeftContainer->content_id;
		
		//INSERTING MAIN-MIDDLE-TAB CONTAINER
		$middleTabContainer = $this->createRow ();
		$middleTabContainer->contentpage_id = $page_id;
		$middleTabContainer->type = 'widget';
		$middleTabContainer->name = 'core.container-tabs';
		$middleTabContainer->parent_content_id = $middle_id;
		$middleTabContainer->order = 7;
		$middleTabContainer->params = "{\"max\":\"$showmaxtab\"}";
		$middleTabContainer->save ();
		$middle_tab = $middleTabContainer->content_id;
		
		//INSERTING THUMB PHOTO WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.thumbphoto-sitepage', $middle_id, 1 );
		
		//INSERTING TITLE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.title-sitepage', $middle_id, 2 );
		
		//INSERTING LIKE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like-button', $middle_id, 3 );
		
		//INSERTING FACEBOOK LIKE WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'facebookse' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', $middle_id, 4 );
		}
		
		//INSERTING PHOTO STRIP WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.photorecent-sitepage', $middle_id, 5 );
		}
		
		//INSERTING MAIN PHOTO WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.mainphoto-sitepage', $left_id, 10 );
		
		//INSERTING OPTIONS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.options-sitepage', $left_id, 11 );
		
		//INSERTING INFORMATION WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.information-sitepage', $left_id, 14, 'Information', 'true' );
		
		//INSERTING WRITE SOMETHING ABOUT WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like', $left_id, 15 );
		
		//INSERTING RATING WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', $left_id, 16, 'Ratings' );
		}
		
		//INSERTING BADGE WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', $left_id, 17, 'Badge' );
		}
		
		//INSERTING YOU MAY ALSO LIKE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.suggestedpage-sitepage', $left_id, 18, 'You May Also Like' );
		
		//INSERTING SOCIAL SHARE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.socialshare-sitepage', $left_id, 19, 'Social Share' );
		
		//INSERTING FOUR SQUARE WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.foursquare-sitepage', $left_id, 20 );
		
		//INSERTING INSIGHTS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.insights-sitepage', $left_id, 21, 'Insights' );
		
		//INSERTING FEATURED OWNER WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.featuredowner-sitepage', $left_id, 22, 'Owners' );
		
		//INSERTING ALBUM WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.albums-sitepage', $left_id, 23, 'Albums' );
		}
		
		//INSERTING PAGE PROFILE PLAYER WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagemusic.profile-player', $left_id, 24 );
		}
		
		//INSERTING LINKED PAGES WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.favourite-page', $left_id, 25, 'Linked Pages' );
		
		//INSERTING ACTIVITY FEED WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'socialengineaddon.feed', $middle_tab, 1, 'Updates', 'true' );
		
		//INSERTING INFORAMTION WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.info-sitepage', $middle_tab, 2, 'Info', 'true' );
		
		//INSERTING OVERVIEW WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.overview-sitepage', $middle_tab, 3, 'Overview', 'true' );
		
		//INSERTING LOCATION WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.location-sitepage', $middle_tab, 4, 'Map', 'true' );
		
		//INSERTING LINKS WIDGET
		$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'core.profile-links', $middle_tab, 125, 'Links', 'true' );
		
		//INSERTING ALBUM WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
			$this->setDefaultInfo ( 'sitepage.photos-sitepage', $page_id, 'Photos', 'true', '110' );
		}
		
		//INSERTING VIDEO WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagevideo' )) {
			$this->setDefaultInfo ( 'sitepagevideo.profile-sitepagevideos', $page_id, 'Videos', 'true', '111' );
		}
		
		//INSERTING NOTE WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagenote' )) {
			$this->setDefaultInfo ( 'sitepagenote.profile-sitepagenotes', $page_id, 'Notes', 'true', '112' );
		}
		
		//INSERTING REVIEW WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			$this->setDefaultInfo ( 'sitepagereview.profile-sitepagereviews', $page_id, 'Reviews', 'true', '113' );
		}
		
		//INSERTING FORM WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageform' )) {
			$this->setDefaultInfo ( 'sitepageform.sitepage-viewform', $page_id, 'Form', 'false', '114' );
		}
		
		//INSERTING DOCUMENT WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagedocument' )) {
			$this->setDefaultInfo ( 'sitepagedocument.profile-sitepagedocuments', $page_id, 'Documents', 'true', '115' );
		}
		
		//INSERTING OFFER WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageoffer' )) {
			$this->setDefaultInfo ( 'sitepageoffer.profile-sitepageoffers', $page_id, 'Offers', 'true', '116' );
		}
		
		//INSERTING EVENT WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageevent' )) {
			$this->setDefaultInfo ( 'sitepageevent.profile-sitepageevents', $page_id, 'Events', 'true', '117' );
		}
		
		//INSERTING POLL WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagepoll' )) {
			$this->setDefaultInfo ( 'sitepagepoll.profile-sitepagepolls', $page_id, 'Polls', 'true', '118' );
		}
		
		//INSERTING DISCUSSION WIDGET
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' )) {
			$this->setDefaultInfo ( 'sitepage.discussion-sitepage', $page_id, 'Discussions', 'true', '119' );
		}
		
		//INSERTING MUSIC WIDGET 
		if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
			$this->setDefaultInfo ( 'sitepagemusic.profile-sitepagemusic', $page_id, 'Music', 'true', '120' );
		}
	}
	
	public function setContentDefault($page_id) {
		
		$pagelayout = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.layout.setting', 1 );
		$showmaxtab = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.showmore', 8 );
		
		if ($pagelayout) {
			//GET CONTENT TABLE
			$contentTable = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' );
			
			//GET CONTENT TABLE NAME
			$contentTableName = $this->info ( 'name' );
			
			//INSERTING MAIN CONTAINER
			$mainContainer = $this->createRow ();
			$mainContainer->contentpage_id = $page_id;
			$mainContainer->type = 'container';
			$mainContainer->name = 'main';
			$mainContainer->order = 2;
			$mainContainer->save ();
			$container_id = $mainContainer->content_id;
			
			//INSERTING MAIN-MIDDLE CONTAINER
			$mainMiddleContainer = $this->createRow ();
			$mainMiddleContainer->contentpage_id = $page_id;
			$mainMiddleContainer->type = 'container';
			$mainMiddleContainer->name = 'middle';
			$mainMiddleContainer->parent_content_id = $container_id;
			$mainMiddleContainer->order = 6;
			$mainMiddleContainer->save ();
			$middle_id = $mainMiddleContainer->content_id;
			
			//INSERTING MAIN-LEFT CONTAINER
			$mainLeftContainer = $this->createRow ();
			$mainLeftContainer->contentpage_id = $page_id;
			$mainLeftContainer->type = 'container';
			$mainLeftContainer->name = 'left';
			$mainLeftContainer->parent_content_id = $container_id;
			$mainLeftContainer->order = 4;
			$mainLeftContainer->save ();
			$left_id = $mainLeftContainer->content_id;
			
			//INSERTING MAIN-MIDDLE TAB CONTAINER
			$middleTabContainer = $this->createRow ();
			$middleTabContainer->contentpage_id = $page_id;
			$middleTabContainer->type = 'widget';
			$middleTabContainer->name = 'core.container-tabs';
			$middleTabContainer->parent_content_id = $middle_id;
			$middleTabContainer->order = 7;
			$middleTabContainer->params = "{\"max\":\"$showmaxtab\"}";
			$middleTabContainer->save ();
			$middle_tab = $middleTabContainer->content_id;
			
			//INSERTING THUMB PHOTO WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.thumbphoto-sitepage', $middle_id, 1 );
			
			//INSERTING TITLE WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.title-sitepage', $middle_id, 2 );
			
			//INSERTING LIKE WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like-button', $middle_id, 3 );
			
			//INSERTING FACEBOOK LIKE WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'facebookse' )) {
				$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', $middle_id, 4 );
			}
			
			//INSERTING PHOTO STRIP WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
				$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.photorecent-sitepage', $middle_id, 5 );
			}
			
			//INSERTING MAIN PHOTO WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.mainphoto-sitepage', $left_id, 10 );
			
			//INSERTING OPTIONS WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.options-sitepage', $left_id, 11 );
			
			//INSERTING INFORMATION WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.information-sitepage', $left_id, 14, 'Information', 'true' );
			
			//INSERTING WRITE SOMETHING ABOUT WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.page-like', $left_id, 15 );
			
			//INSERTING RATING WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
				$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', $left_id, 16, 'Ratings' );
			}
			
			//INSERTING BADGE WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' )) {
				$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', $left_id, 17, 'Badge' );
			}
			
			//INSERTING YOU MAY ALSO LIKE WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.suggestedpage-sitepage', $left_id, 18, 'You May Also Like' );
			
			//INSERTING SOCIAL SHARE WIDGET
			$this->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.socialshare-sitepage', $left_id, 19, 'Social Share' );
			
			//INSERTING FOUR SQUARE WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.foursquare-sitepage', $left_id, 20 );
			
			//INSERTING INSIGHTS WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.insights-sitepage', $left_id, 21, 'Insights' );
			
			//INSERTING FEATURED OWNER WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.featuredowner-sitepage', $left_id, 22, 'Owners' );
			
			//INSERTING ALBUM WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
				Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.albums-sitepage', $left_id, 23, 'Albums' );
			}
			
			//INSERTING PAGE PROFILE PLAYER WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
				Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepagemusic.profile-player', $left_id, 24 );
			}
			
			//INSERTING LINKED PAGES WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.favourite-page', $left_id, 25, 'Linked Pages' );
			
			//INSERTING ACTIVITY FEED WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'socialengineaddon.feed', $middle_tab, 1, 'Updates', 'true' );
			
			//INSERTING INFORAMTION WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.info-sitepage', $middle_tab, 2, 'Info', 'true' );
			
			//INSERTING OVERVIEW WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.overview-sitepage', $middle_tab, 3, 'Overview', 'true' );
			
			//INSERTING LOCATION WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'sitepage.location-sitepage', $middle_tab, 4, 'Map', 'true' );
			
			//INSERTING LINKS WIDGET
			Engine_Api::_ ()->getDbTable ( 'content', 'sitepage' )->setDefaultDataUserWidget ( $contentTable, $contentTableName, $page_id, 'widget', 'core.profile-links', $middle_tab, 125, 'Links', 'true' );
			
			//INSERTING ALBUM WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepage.photos-sitepage', $page_id, 'Photos', 'true', '110' );
			}
			
			//INSERTING VIDEO WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagevideo' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagevideo.profile-sitepagevideos', $page_id, 'Videos', 'true', '111' );
			}
			
			//INSERTING NOTE WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagenote' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagenote.profile-sitepagenotes', $page_id, 'Notes', 'true', '112' );
			}
			
			//INSERTING REVIEW WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagereview.profile-sitepagereviews', $page_id, 'Reviews', 'true', '113' );
			}
			
			//INSERTING FORM WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageform' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepageform.sitepage-viewform', $page_id, 'Form', 'false', '114' );
			}
			
			//INSERTING DOCUMENT WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagedocument' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagedocument.profile-sitepagedocuments', $page_id, 'Documents', 'true', '115' );
			}
			
			//INSERTING OFFER WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageoffer' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepageoffer.profile-sitepageoffers', $page_id, 'Offer', 'true', '116' );
			}
			
			//INSERTING EVENT WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepageevent' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepageevent.profile-sitepageevents', $page_id, 'Events', 'true', '117' );
			}
			
			//INSERTING POLL WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagepoll' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagepoll.profile-sitepagepolls', $page_id, 'Polls', 'true', '118' );
			}
			
			//INSERTING DISCUSSION WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepage.discussion-sitepage', $page_id, 'Discussions', 'true', '119' );
			}
			
			//INSERTING NOTE WIDGET
			if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagemusic' )) {
				Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setDefaultInfo ( 'sitepagemusic.profile-sitepagemusic', $page_id, 'Music', 'true', '120' );
			}
		} else {
			Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->setWithoutTabLayout ( $page_id );
		}
	}
	
	/**
	 * Gets content id,parama,name
	 *
	 * @param int $contentpage_id
	 * @return content id,parama,name
	 */
	public function getContents($contentpage_id) {
		
		$selectPageAdmin = $this->select ()->from ( $this->info ( 'name' ), array ('content_id', 'params', 'name' ) )->where ( 'contentpage_id =?', $contentpage_id )->where ( "name IN ('sitepage.overview-sitepage', 'sitepage.photos-sitepage', 'sitepage.discussion-sitepage', 'sitepagenote.profile-sitepagenotes', 'sitepagepoll.profile-sitepagepolls', 'sitepageevent.profile-sitepageevents', 'sitepagevideo.profile-sitepagevideos', 'sitepageoffer.profile-sitepageoffers', 'sitepagereview.profile-sitepagereviews', 'sitepagedocument.profile-sitepagedocuments', 'sitepageform.sitepage-viewform','sitepage.info-sitepage', 'socialengineaddon.feed', 'activity.feed', 'sitepage.location-sitepage', 'core.profile-links', 'sitepagemusic.profile-sitepagemusic')" );
		return $this->fetchAll ( $selectPageAdmin );
	}
	
	/**
	 * Gets content_id
	 *
	 * @param int $contentpage_id
	 * @return $params
	 */
	public function getContentId($contentpage_id, $sitepage) {
		$select = $this->select ();
		$select_content = $select->from ( $this->info ( 'name' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'main' )->limit ( 1 );
		$content = $select_content->query ()->fetchAll ();
		if (! empty ( $content )) {
			$select = $this->select ();
			$select_container = $select->from ( $this->info ( 'name' ), array ('content_id' ) )->where ( 'contentpage_id = ?', $contentpage_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->where ( "name NOT IN ('	sitepage.title-sitepage', 'sitepage.page-like-button', 'sitepage.photorecent-sitepage')" )->limit ( 1 );
			$container = $select_container->query ()->fetchAll ();
			if (! empty ( $container )) {
				$select = $this->select ();
				$container_id = $container [0] ['content_id'];
				$select_middle = $select->from ( $this->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'contentpage_id = ?', $contentpage_id )->limit ( 1 );
				$middle = $select_middle->query ()->fetchAll ();
				if (! empty ( $middle )) {
					$content_id = $middle [0] ['content_id'];
				} else {
					$content_id = $container_id;
				}
			}
		}
		
		if (! empty ( $content_id )) {
			$select = $this->select ();
			$select_middle = $select->from ( $this->info ( 'name' ), array ('content_id', 'name' ) )->where ( 'parent_content_id = ?', $content_id )->where ( 'type = ?', 'widget' )->where ( "name NOT IN ('sitepage.title-sitepage', 'sitepage.page-like-button', 'sitepage.photorecent-sitepage', 'Facebookse.facebookse-sitepageprofilelike', 'sitepage.thumbphoto-sitepage')" )->where ( 'contentpage_id = ?', $contentpage_id )->order ( 'order ASC' );
			
			$middle = $select_middle->query ()->fetchAll ();
			$editpermission = '';
			$isManageAdmin = '';
			$content_ids = '';
			$content_names = '';
			$flag = false;
			foreach ( $middle as $value ) {
				$content_name = $value ['name'];
				switch ($content_name) {
					case 'sitepage.overview-sitepage' :
						if (! empty ( $sitepage )) {
							$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'overview' );
							if (! empty ( $isManageAdmin )) {
								$editpermission = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
								if (! empty ( $editpermission ) && empty ( $sitepage->overview )) {
									$flag = true;
								} elseif (empty ( $editpermission ) && empty ( $sitepage->overview )) {
									$flag = false;
								} elseif (! empty ( $editpermission ) && ! empty ( $sitepage->overview )) {
									$flag = true;
								} elseif (empty ( $editpermission ) && ! empty ( $sitepage->overview )) {
									$flag = true;
								}
							}
						}
						break;
					case 'sitepage.location-sitepage' :
						if (! empty ( $sitepage )) {
							$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'map' );
							if (! empty ( $isManageAdmin )) {
								$value ['id'] = $sitepage->getIdentity ();
								$location = Engine_Api::_ ()->getDbtable ( 'locations', 'sitepage' )->getLocation ( $value );
								if (! empty ( $location )) {
									$flag = true;
								}
							}
						}
						break;
					case 'activity.feed' :
						$flag = true;
						break;
					case 'socialengineaddon.feed' :
						$flag = true;
						break;
					case 'sitepage.info-sitepage' :
						$flag = true;
						break;
					case 'core.profile-links' :
						$flag = true;
						break;
					case 'sitepagenote.profile-sitepagenotes' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagenote" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'sncreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepageevent.profile-sitepageevents' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageevent" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'secreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepage.discussion-sitepage' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagediscussion" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'comment' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepage.photos-sitepage' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagealbum" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'spcreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepagemusic.profile-sitepagemusic' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagemusic" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'smcreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepagedocument.profile-sitepagedocuments' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagedocument" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'sdcreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepagereview.profile-sitepagereviews' :
						$flag = true;
						break;
					case 'sitepagevideo.profile-sitepagevideos' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagevideo" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'svcreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepagepoll.profile-sitepagepolls' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagepoll" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'splcreate' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepageoffer.profile-sitepageoffers' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageoffer" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'offer' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
					case 'sitepageform.sitepage-viewform' :
						if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
							if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageform" ) == 1) {
								$flag = true;
							}
						} else {
							$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'form' );
							if (! empty ( $isPageOwnerAllow )) {
								$flag = true;
							}
						}
						break;
				}
				if (! empty ( $flag )) {
					$content_ids = $value ['content_id'];
					$content_names = $value ['name'];
					break;
				}
			}
		}
		
		return array ('content_id' => $content_ids, 'content_name' => $content_names );
	}
	
	/**
	 * Gets content_id, name
	 *
	 * @param int $contentpage_id
	 * @return content_id, name
	 */
	public function getContentInformation($contentpage_id) {
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('content_id', 'name' ) )->where ( "name IN ('activity.feed', 'core.profile-links', 'socialengineaddon.feed')" )->where ( 'contentpage_id = ?', $contentpage_id );
		
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Gets content_id, name
	 *
	 * @param int $contentpage_id
	 * @param int $name 
	 * @return content_id, name
	 */
	public function getContentByWidgetName($name, $contentpage_id) {
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('content_id', 'name' ) )->where ( 'name =?', $name )->where ( 'contentpage_id = ?', $contentpage_id )->limit ( 1 );
		return $this->fetchAll ( $select )->toarray ();
	}
	
	/**
	 * Gets name
	 *
	 * @param int $tab_main
	 * @return name
	 */
	public function getCurrentTabName($tab_main = null) {
		if (empty ( $tab_main )) {
			return;
		}
		$current_tab_name = $this->select ()->from ( $this->info ( 'name' ), array ('name' ) )->where ( 'content_id = ?', $tab_main )->query ()->fetchColumn ();
		return $current_tab_name;
	}

}
?>